package edu.nf.project.configs;

import edu.nf.project.entitys.entity.accountsAndPermissions.convention.Users;
import edu.nf.project.services.accountsAndPermissions.UsersService;
import edu.nf.project.services.accountsAndPermissions.impl.UsersServiceImpl;
import edu.nf.project.utils.SHA256Util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author CSY
 * @date 2025 - 06 - 11
 * 对数据库的所有用户密码进行加密
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class);
        UsersService bean = context.getBean(UsersService.class);
        List<Users> allUsers = bean.getAllUsers();

        // 2. 处理用户列表
        List<Users> list = allUsers.stream().map(user -> {
            Users newUser = new Users();
            newUser.setUId(user.getUId());
            newUser.setPwd(user.getPwd());
            return newUser;
        }).toList();

        // 3. 更新用户密码
        list.forEach(bean::updateUser);

    }

    private static String findPlaintextForHash(String hash) {
        // 实际应用中不应暴力破解，此处仅为演示
        String[] possiblePasswords = {"123456", "1234567", "password", "12345678", "12345"};
        for (String pwd : possiblePasswords) {
            if (hash.equals(SHA256Util.encrypt(pwd))) {
                return pwd;
            }
        }
        return "未知";
    }
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hex = new StringBuilder();
        for (byte b : bytes) {
            hex.append(String.format("%02x", b));
        }
        return hex.toString();
    }
}
